package com.example.provider.kafka;

import com.example.api.common.message.KafkaMessage;
import com.example.api.common.trace.TraceIdUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

@Component
public class UserRegisterListener {
    private static final Logger logger = LoggerFactory.getLogger(UserRegisterListener.class);
    private static final ObjectMapper objectMapper = new ObjectMapper();
    
    @KafkaListener(topics = "user-register")
    public void onMessage(String message) {
        try {
            KafkaMessage<String> kafkaMessage = objectMapper.readValue(message, new TypeReference<KafkaMessage<String>>() {});
            if (kafkaMessage.getTraceId() != null) {
                TraceIdUtil.setTraceId(kafkaMessage.getTraceId());
            }
            logger.info("Received user register message: {}", kafkaMessage.getData());
            // 处理用户注册后的业务逻辑
        } catch (Exception e) {
            logger.error("Failed to process user register message", e);
        } finally {
            TraceIdUtil.clearTraceId();
        }
    }
} 